package com.example.demo.utils;

import com.example.demo.annotation.AsExcelCell;
import com.example.demo.annotation.AsExcelCellList;
import com.example.demo.dto.ProductStatisticsVO;
import com.example.demo.dto.ProductVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Font;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * excel 工具类
 * Created by gan on 2018/8/29 22:39.
 */
public class ExcelUtil {

    /**
     * excel 导出封装
     *
     * @param headers
     */
    public static HSSFWorkbook export(String[] headers, List<ProductVO> datas) throws Exception {
        // 声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();

        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet();

        // 设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth((short) 18);

        //第一行设置标题
        HSSFRow titleRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = titleRow.createCell(i);


            HSSFCellStyle cellStyle = workbook.createCellStyle();  //生成单元格样式
            HSSFFont hssfFont = workbook.createFont();  //新建font实体
            hssfFont.setColor(HSSFColor.BLACK.index);   //设置字体颜色
            hssfFont.setFontHeightInPoints((short) 20); //字体大小
            hssfFont.setFontName("楷体");
            hssfFont.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体
            cellStyle.setFont(hssfFont);

            cell.setCellStyle(cellStyle);

            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }




//        for(int i = 1;i<=headers.length;i++){
//            for (int j = 0; j < datas.size(); j++) {
//                ProductVO productVO = datas.get(j);
//                String textValue = StringUtils.EMPTY;
//                HSSFRow row = sheet.createRow(i);
//
//
//                HSSFCell cell = row.createCell(i-1);
//                HSSFRichTextString richString = new HSSFRichTextString(textValue);
//                HSSFFont font3 = workbook.createFont();
//                font3.setColor(HSSFColor.BLACK.index);//定义Excel数据颜色
//                richString.applyFont(font3);
//                cell.setCellValue(richString);
//            }
//
//        }

        for (int i = 1; i <= datas.size(); i++) {
            ProductVO productVO = datas.get(i - 1);
            Field[] productVOFields = ProductVO.class.getDeclaredFields();
            String textValue = StringUtils.EMPTY;

            HSSFRow row = sheet.createRow(i);
            for (int j = 0; j < productVOFields.length; j++) {
                Field productVOField = productVOFields[j];
                HSSFCell cell = row.createCell(j);

                if (productVOField.isAnnotationPresent(AsExcelCell.class)) {
                    productVOField.setAccessible(true);
                    Object o = productVOField.get(productVO);
                    if (o instanceof Date) {
                        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
                        textValue = sf.format(o);
                    } else {
                        textValue = StringUtils.EMPTY + o;
                    }

                    System.out.println("============>" + textValue);


                    HSSFRichTextString richString = new HSSFRichTextString(textValue);
                    HSSFFont font3 = workbook.createFont();
                    font3.setColor(HSSFColor.BLACK.index);//定义Excel数据颜色
                    richString.applyFont(font3);
                    cell.setCellValue(richString);

                } else if (productVOField.isAnnotationPresent(AsExcelCellList.class)) {
                    List<ProductStatisticsVO> vos = productVO.getVos();
                    if (!CollectionUtils.isEmpty(vos)) {
                        for (int k = 0; k < vos.size(); k++) {
                            ProductStatisticsVO productStatisticsVO = vos.get(k);
                            Field[] productStatisticsVOFields = ProductStatisticsVO.class.getDeclaredFields();
                            for (int l = 0; l < productStatisticsVOFields.length; l++) {
                                Field productStatisticsVOField = productStatisticsVOFields[l];
                                productStatisticsVOField.setAccessible(true);
                                Object o = productStatisticsVOField.get(productStatisticsVO);
                                if (o instanceof Date) {
                                    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
                                    textValue = sf.format(o);
                                } else {
                                    textValue = StringUtils.EMPTY + o;
                                }
                            }
                        }

                        System.out.println("============>" + textValue);
                        HSSFRichTextString richString = new HSSFRichTextString(textValue);
                        HSSFFont font3 = workbook.createFont();
                        font3.setColor(HSSFColor.BLACK.index);//定义Excel数据颜色
                        richString.applyFont(font3);
                        cell.setCellValue(richString);
                    }


                }
            }
        }

        return workbook;

    }


}
